<?php 
namespace app\user\controller;
use think\Request;
use think\Loader;
use app\common\controller\Api;
use app\user\traits\controllers\TraitUser;
use think\Lang;
/**
 * 会员心愿类
 */
class Wish extends Api
{
	use TraitUser;
	//前置操作
    protected $beforeActionList = [
        'checkLogin'
    ];
    /**
     * [checkLogin 判断是否登陆]
     * @return [type] [description]
     */
    protected function checkLogin()
    {
        if (!model('Users')->checkLogin()) {
            \think\Session::set('redirect_url', Request::instance()->url(true));
            $this->redirect('@user/login');
            exit;
        }
    }
	/**
     * [FunctionName 城市列表数组]
     * @param string $value [description]
     */
    public function getHotCity()
    {
        $region = [
        	'Beijing'  => Lang::get('beijing'), 
    		'Shanghai' => Lang::get('shanghai'),
    		'Sanya'    => Lang::get('sanya'),
    		'Chengdu'  => Lang::get('chengdu'),
    		'Xian'     => Lang::get('xian')
		];
        return $region;
    }
	
	/**
	 * 用户自己的心愿列表
	 */
	public function getIndex(){
		$list = $this->getWishs();
		return view('wish/mywishlist',['list'=>$list]);
	}
	
	/**
	 * 添加心源表单
	 */
	public function getCreate(){
		$region = $this->getHotCity();
		return view('wish/publishwish',['region'=>$region]);
	}
	
	/**
	 * 添加心源
	 */
	public function postSave(Request $request){
		$this->createwish($request->post());
		return json(['code'=>1]);
	}
	
	/**
	 * 编辑心愿表单
	 */
	public function getEdit(Request $request){
		$data = $this->user->getWishInfo(['id'=>$request->get('id/d')]);
		$region = $this->getHotCity();
	    return view('wish/updatewish',['data'=>$data,'region'=>$region]);
	}
	/**
	 * 编辑心愿
	 * @param $where 条件
	 */
	public function postUpdate(Request $request){
		$this->user->updateWish($id = $request->post('id'),$request->post());
		return json(['code'=>1]);
	}
	
	/**
	 * 取消发布心愿
	 */
	public function getCancelWish(){
		$result = $this->user->cancelWishs($this->request->get('id'));
		$this->redirect('/wishs/index');
	}
	
	/**
	 * 心愿接单房车列表
	 */
	public function getAcceptOrderCaravan(){
		$list = Loader::model('MeetWishUsers')->with('caravans')->where(['wish_id'=>$this->request->get('wid/d')])->select();
		return $this->fetch('wish/acceptordercaravan',['list'=>$list]);
	}
	
	/**
	 * 全部心愿列表
	 */
	public function getAllWishList(Request $request){
		empty($request->get('start_date')) || empty($request->get('end_date')) ? $date = [] : $date = $request->get();
		$list = $this->getOtherwishs($date);
		return view('wish/wishlist',['list'=>$list]);
	}	
	
	/**
	 * ajax获取更多心愿
	 */
	public function postGetManyWish(Request $request){
		if(empty($request->post('start_date')) || empty($request->post('end_date'))){
			$date = [];
		}else{
			$date = ['start_date'=>$request->post('start_date'),'end_date'=>$request->post('end_date')];
		}
		return json($this->getOtherwishs($date,$request->post('page')));
	}
	/**
	 * 接单
	 */
	public function postAcceptOrder(){
		$caravan_id = Loader::model('Caravans')->where(['user_id'=>Request::instance()->user->id])->value('id');
		$this->user->meetWish($caravan_id,$this->request->get('id/d'));
		$this->redirect('/wishs/myacceptorder');
	}
	
	/**
	 * 我的接单列表
	 */
	public function getMyAcceptOrder(){
		$list = model('MeetWishUsers')->getWishs(['meet_user_id' => Request::instance()->user->id],'Wishs');
	    return view('wish/myacceptorder',['list'=>$list]);
	}
}

 ?>